﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;

namespace QuanLyKho.PhongGiaCong
{
    public partial class DanhSachCat : Form
    {
        public DanhSachCat()
        {
            InitializeComponent();
            LoadData();
            SetPropertiesGrid();
        }

        public void LoadData()
        {
            DataTable dtMau = DataAll.SelectAll("Cat inner join PhongMau_SanPham on Cat.IdSanPham = PhongMau_SanPham.IdSanPham",
                    "PhongMau_SanPham.[MaHang] as MaSanPham,PhongMau_SanPham.[LoaiHang],PhongMau_SanPham.[LanSanXuat],PhongMau_SanPham.[LenhSanXuat],Cat.[ID],Cat.[IdSanPham],Cat.[NgayLapLenh],Cat.[NguoiCapNhatLenh],Cat.[SoLuong],Cat.[NgayGuiLenh],Cat.[NgaySuaLenh],Cat.[TrangThai],Cat.[NgayChungTu],Cat.[NgayHoachToan],Cat.[SoChungTu],Cat.[GhiChu],Cat.[KhoDen],Cat.[KhoDi],Cat.[SXTuNgay],Cat.[SXDenNgay]",
                    "WHERE Cat.TrangThai = " + Convert.ToInt32(TrangThai.Cat_GuiGiaCong));
            gridCat.DataSource = dtMau;
        }

        public void SetPropertiesGrid()
        {

            gvCat.BestFitColumns();
            gvCat.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            gvCat.OptionsBehavior.Editable = false;
            gvCat.OptionsSelection.MultiSelect = true;
            gvCat.OptionsView.ShowGroupPanel = false;
            gvCat.OptionsView.ShowFooter = true;
            gvCat.OptionsFind.AlwaysVisible = true;
            gvCat.Columns["ID"].Visible = false;
        }

        private void gvCat_DoubleClick(object sender, EventArgs e)
        {
            var dataview = (DataView)gvCat.DataSource;
            var data = DataAll.SelectAll("HanhTrinh", "*", " WHERE SoChungTu='" + dataview[gvCat.FocusedRowHandle]["SoChungTu"]
                + "' AND TrangThai=" + Convert.ToInt32(TrangThai.Cat_GiaCongDaNhan));
            if (data.Rows.Count <= 0)
            {
                DataAll.InsertAll("HanhTrinh", dataview[gvCat.FocusedRowHandle]["MaSanPham"]
                            + "," + Convert.ToInt32(PhongBan.PhongGiaCong)
                            + "," + dataview[gvCat.FocusedRowHandle]["SoChungTu"]
                            + "," + Convert.ToInt32(TrangThai.Cat_GiaCongDaNhan)
                            + "," + dataview[gvCat.FocusedRowHandle]["NguoiCapNhatLenh"]
                            + "," + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")
                            + "," + dataview[gvCat.FocusedRowHandle]["SoLuong"]
                            + "," + Convert.ToDateTime(dataview[gvCat.FocusedRowHandle]["TuNgay"].ToString()).ToString("MM/dd/yyyy")
                            + "," + Convert.ToDateTime(dataview[gvCat.FocusedRowHandle]["DenNgay"].ToString()).ToString("MM/dd/yyyy"));
            }
            Giacong_ThemGiacong.IdCat = gvCat.Columns["ID"].View.GetRowCellDisplayText(gvCat.FocusedRowHandle, "ID");
            var lss = new Giacong_ThemGiacong();
            lss.ShowDialog();
            LoadData();
            Giacong_ThemGiacong.IdCat = "0";
        }

        private void gvCat_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "SXDenNgay")
            {
                TimeSpan songay = Convert.ToDateTime(e.CellValue) - DateTime.Now;
                if (0 < songay.Days && songay.Days < 5)
                {
                    e.Appearance.BackColor = Color.PaleGoldenrod;
                }
                if (songay.Days < 0)
                {
                    e.Appearance.BackColor = Color.DarkGray;
                }
            }
        }
    }
}
